package com.lw.mall.service2.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lw.mall.entity.Order;
import com.lw.mall.entity.OrderItem;
import com.lw.mall.entity.User;
import com.lw.mall.filter.AuthorizationFilter;
import com.lw.mall.mapper.OrderItemMapper;
import com.lw.mall.mapper.OrderMapper;
import com.lw.mall.service2.OrderService2;
import com.lw.mall.vo.PayAsyncVo;
import com.lw.mall.vo.SellListVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lw
 * @since 2022-09-25
 */
@Service("orderServiceImpl2")
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService2 {

    @Resource
    private OrderItemMapper orderItemMapper;
    /**
     *
     * @param orderIdList 订单id
     * @return
     */
    @Override
    public List<SellListVo> getOrderSellListByIds(List<Integer> orderIdList) {
        final User user = AuthorizationFilter.userLogin.get();
        final Integer userId = user.getId();//当前登录用户
        final List<Order> list = baseMapper.selectBatchIds(orderIdList);
        final List<SellListVo> collect = list.stream().map(item -> {
            //订单id
            final Integer orderId = item.getId();
            final SellListVo sellListVo = new SellListVo();
            BeanUtils.copyProperties(item, sellListVo);
            final OrderItem orderItem = orderItemMapper.
                    selectOne(new LambdaQueryWrapper<OrderItem>().eq(OrderItem::getOrderId, orderId).eq(OrderItem::getSellId, userId));
            sellListVo.setTotal(orderItem.getSubTotal());//金额
            return sellListVo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public String handlePayResult(PayAsyncVo payAsyncVo) {
        //订单记录 保存到数据库中
        //...
        return "success";
    }
}
